<template>
	<view class="">
		<view class="box1" style="display: flex;justify-content: space-between;">
			<view class="info1">
				<text>商品评分:</text>
				<star-rating :initial-rating="3" @ratingChanged="handleRatingChange1"></star-rating>
			</view>
		</view>
		<view class="box2">
			<textarea v-bind:maxlength="Surplus" @input="descArea" v-model="inputText" name="abstract" id="abstract"
				placeholder="宝贝满足你的期待吗？说说你的使用心得，分享给想买的他们吧！"></textarea>
		</view>
		<view class="box3" style="display: flex;justify-content: space-between;">
			<view class="info2">
				<text>服务态度:</text>
				<star-rating :initial-rating="3" @ratingChanged="handleRatingChange2"></star-rating>
			</view>
			<button type="primary" class="confirm" @click="insertRates">发布</button>
		</view>


	</view>
</template>

<script>
	import axios from 'axios';
	import service from '../../utils/request.js';
	import StarRating from '@/components/star-rating.vue';

	export default {
		components: {
			StarRating
		},
		data() {
			return {
				currentRating1: 3,
				currentRating2: 3,
				goodsId: null,
				sellerId: null,
				userName:'',
				userInfo:'',
				inputText: ''
			};
		},
		onLoad(options) {
			// 获取 URL 查询参数
			this.goodsId = options.goodsID;
			this.sellerId = options.sellerID;
		},
		methods: {
			handleRatingChange1(newRating) {
				console.log('Received rating:', newRating); // 打印传递的评分值
				console.log(this.sellerId);
				this.currentRating1 = newRating;
			},
			handleRatingChange2(newRating) {
				console.log('Received rating:', newRating); // 打印传递的评分值
				console.log(this.goodsId);
				this.currentRating2 = newRating;
			},
			async insertRates() {
				try {
					this.userID = localStorage.getItem("userID");
					const response = await service.post('/rates/doRate', { // 你的后端地址
						sellerId: this.sellerId,
						userId: this.userID,
						userName:this.userInfo.username,
						userImg:this.userInfo.userPic,
						rate: this.inputText,
						goodsID:this.goodsId
					});

					uni.showToast({
						title: '成功',
						icon: 'success'
					});
					uni.navigateTo({
						url: '/pages/order/order'
					})
				} catch (error) {
					console.error('插入失败:', error);
					uni.showToast({
						title: '失败',
						icon: 'none'
					});
				}
			},
			async getUserInfo() {
				try {
					// 确认 goodsID 的类型是整数
					this.userID = localStorage.getItem("userID");
					const response5 = await service.get('/user/' +this.userID );
					this.userInfo=response5.data;
	                console.log(response5);

				} catch (error) {
					console.error('获取数据失败:', error);
					console.log('Received goodsID:', this.goodsID);
				}
			},

		},
		mounted() {
			// 页面加载完成后，可以调用方法来获取商品详情

		},
		onShow() {
			this.getUserInfo();
		}
	};
</script>
<style lang="scss" scoped>
	@use "appraise";
</style>